package com.firefly.app.cordova.plugins;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.firefly.mobile.app.main.FireflyMobileActivity;
import com.firefly.mobile.app.util.SytLog;

public class CallPhonePlugin  extends CordovaPlugin implements CordovaInterface{
	private final ExecutorService threadPool = Executors.newCachedThreadPool(); 
	    /** 
	     * 注意 构造方法不能为 
	     *  
	     * Plugin_intent(){} 
	     *  
	     * 可以不写或者 定义为如下 
	     *  
	     */  
	    public CallPhonePlugin() {  
	    }  
	  
	  
	    @Override  
	    public boolean execute(final String action,final  org.json.JSONArray args,  
	    		final  CallbackContext callbackContext) throws org.json.JSONException {  
	        SytLog.i("进入统一代理ajax,callPhonePlugin", action);  
	        
	        cordova.getThreadPool().execute(new Runnable() {
	            public void run() {
	                try {
						execPlugin(action, args, callbackContext);
					} catch (Exception e) {
						e.printStackTrace();
						SytLog.e("packPlugin", "插件调用出错");
						callbackContext.error("插件调用出错");
					}
	            }
	        });
	        
	        return true;  
	  
	    }  
	    
	    
	    private void execPlugin(String action, org.json.JSONArray args,  
	            CallbackContext callbackContext) throws org.json.JSONException {
		        if(action.equals("callPhone")){
		        	String phoneNum = args.getString(0);
	                //用intent启动拨打电话  
	                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNum));  
	                getActivity().startActivity(intent);  
		        }
	    }
	    
		@Override
		public void startActivityForResult(CordovaPlugin command,
				Intent intent, int requestCode) {
			
		}

		@Override
		public void setActivityResultCallback(CordovaPlugin plugin) {
			
		}

		@Override
		public Activity getActivity() {
			return this.cordova.getActivity();
		}

		@Override
		public ExecutorService getThreadPool() {
			return threadPool;
		}
	    
}
